package com.campus.controller;

import com.campus.dto.ActivityCreateDto;
import com.campus.dto.ActivityQueryDto;
import com.campus.model.Activity;
import com.campus.service.ActivityService;
import com.campus.web.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.campus.common.BusinessConstants.API_BASE;

@RestController
@RequestMapping("/activities")
public class ActivityController {

    private final ActivityService activityService;

    public ActivityController(ActivityService activityService) {
        this.activityService = activityService;
    }


    @PostMapping("/create")
    public ResponseEntity<Activity> createActivity(@RequestBody ActivityCreateDto activityCreateDto) {
        Activity activity = activityService.createActivity(activityCreateDto);
        return ResponseEntity.ok(activity);
    }


    @PutMapping("/{id}/update")
    public ResponseEntity<Activity> updateActivity(
            @PathVariable("id") int id,
            @RequestBody ActivityCreateDto activityCreateDto) {
        Activity updatedActivity = activityService.updateActivity(id, activityCreateDto);
        return ResponseEntity.ok(updatedActivity);
    }

    @PostMapping("/{id}/publish")
    public ResponseEntity<String> publishActivity(@PathVariable("id") int id) {
        boolean success = activityService.publishActivity(id);
        if (success) {
            return ResponseEntity.ok("活动已成功发布");
        } else {
            return ResponseEntity.status(400).body("活动发布失败");
        }
    }

    @GetMapping("/query")
    public ResponseEntity<List<Activity>> queryActivities(
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "nameKeyword", required = false) String nameKeyword,
            @RequestParam(value = "sortBy", defaultValue = "created_time") String sortBy) {
        ActivityQueryDto queryDto = new ActivityQueryDto();
        queryDto.setStatus(status);
        queryDto.setNameKeyword(nameKeyword);
        queryDto.setSortBy(sortBy);

        List<Activity> activities = activityService.queryActivities(queryDto);
        return ResponseEntity.ok(activities);
    }
}